﻿using System.Collections;
using System.IO;
using NUnit.Framework;
using System.Collections.Generic;

namespace NUnit4PowerShell
{
    public class TestsRunner
    {
        /// <summary>
        /// This method add a fixture in the suite of Fixture to execute 
        /// and add the script file name to test in the FixtureProvider instance
        /// </summary>
        /// <param name="name"></param>
        /// <param name="suite"></param>
        /// <returns></returns>
        private static IList AddFixture(string name, IList suite)
        {
            Logger.Instance.Info("Add Fixture " + name);
            FixtureProvider.AddOne(name);
            suite.Add(new ExecutePowerShellFixture());
            return suite;
        }

        /// <summary>
        /// This property contains the fixture list to execute
        /// </summary>
        [Suite]
        public static IEnumerable Suite
        {
            get
            {
                Logger.Instance.Info("[TestsRunner] Start to look for Fixture to execute");
                //System.Threading.Thread.Sleep(10000);
                var suite = new ArrayList();

                // We keep the name of the fixture file to execute
                List<string> listFixture = new List<string>();

                // We get all powershell files to execute from the folder specified in the configuration file
                var psFixtureFileListToExecute = Utils.GetPSFixtureFiles();
                // Instancia
                foreach (var file in psFixtureFileListToExecute)
                {
                    FileInfo fileInfo = new FileInfo(file);
                    AddFixture(fileInfo.Name, suite);
                    listFixture.Add(fileInfo.Name);
                }

                // To put them back inside the Fixture Provider -> NUnit instantiate them twice
                // Additionnaly the list has been reversed to be executed alphabeticly sorted by name
                foreach(string item in listFixture)
                {
                    FixtureProvider.AddOne(item);
                }

                Logger.Instance.Info("[TestsRunner] End of the Fixture Suite definition");
                Logger.Instance.AddEmptyLine();

                return suite;
            }
        }
    }
}
